package com.mew.slots.core.common.handler;

import com.mew.common.rest.bo.GameSession;
import com.mew.slots.core.common.service.BetService;
import com.mew.common.exception.ResponseCode;
import com.mew.slots.core.common.messge.request.BetRequestMessage;
import com.mew.slots.core.common.messge.request.BonusRequestMessage;
import com.mew.slots.core.common.messge.response.SlotsBonusPlayResponseMessage;
import com.mew.slots.core.common.messge.response.SlotsPlayResponseMessage;
import com.mew.slots.core.common.vo.StrategyGameVO;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public abstract class AbstractBetHandler implements BetHandler {

    @Autowired
    private BetService slotsBetService;

    @Override
    public void bet(ZebraCommand<BetRequestMessage> command, GameSession userVO) {
            //---------校验-------------
            StrategyGameVO gameVO = slotsBetService.commonCheck(command.getRequest(), userVO);
            if (ResponseCode.Success.code() != gameVO.getStat()) {
                command.sendFailureCode(gameVO.getStat());
                return;
            }
            //调用自定义特殊校验
            specialCheck(gameVO, userVO);
            if (ResponseCode.Success.code() != gameVO.getStat()) {
                command.sendFailureCode(gameVO.getStat());
                return;
            }

            //----------游戏-之前调用测录------
            slotsBetService.betBefore(gameVO, userVO);
            if (gameVO.getStat() != ResponseCode.Success.code()) {
                command.sendFailureCode(gameVO.getStat());
                return;
            }

            //----------游戏-------
            SlotsPlayResponseMessage message = play(gameVO, userVO);

            if (gameVO.getStat() != ResponseCode.Success.code()) {
                command.sendFailureCode(gameVO.getStat());
                return;
            }
            //-------游戏之后写数据-------
            slotsBetService.betAfter(gameVO, userVO, message);
            log.debug("Response:{} StrategyGameVO:{}", message.toString(), gameVO.toString());
            slotsBetService.log(gameVO,userVO);
            command.sendMessage(message);
    }


    @Override
    public void bonusBet(ZebraCommand<BonusRequestMessage> command, GameSession userVO) {
        // 公共校验
        StrategyGameVO gameVO = slotsBetService.commonBonusCheck(command.getRequest(), userVO);
        if (ResponseCode.Success.code() != gameVO.getStat()){
            command.sendFailureCode(gameVO.getStat());
            return;
        }

        // TODO  特殊 不需要走测录
        SlotsBonusPlayResponseMessage message = playBonus(gameVO, userVO);
        if (gameVO.getStat() != ResponseCode.Success.code()){
            command.sendFailureCode(gameVO.getStat());
            return;
        }

        slotsBetService.bonusAfter(gameVO, userVO, message);
        slotsBetService.log(gameVO,userVO);
        log.debug("BonusResponse:{} StrategyGameVO:{}",message.toString(),gameVO.toString());
        command.sendMessage(message);

    }


    public abstract void specialCheck(StrategyGameVO gameVO, GameSession userVO);

    public abstract SlotsPlayResponseMessage play(StrategyGameVO gameVO, GameSession userVO);

    public abstract SlotsBonusPlayResponseMessage playBonus(StrategyGameVO gameVO, GameSession userVO);

}
